package it.polimi.traveldream.beans;

import it.polimi.traveldream.dto.CartPackageDTO;
import it.polimi.traveldream.ejb.CustomerManager;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean(name = "cartBean")
@ViewScoped
public class CartBean {

	@EJB
	private CustomerManager manager;

	private List<CartPackageDTO> packages;

	@PostConstruct
	public void init() {
		this.packages = this.manager.getCart();
	}

	public List<CartPackageDTO> getPackages() {
		return this.packages;
	}

	public void setPackages(List<CartPackageDTO> packages) {
		this.packages = packages;
	}

	public String remove(CartPackageDTO pack) {
		this.packages.remove(pack);
		this.manager.remove(pack);
		return null;
	}

	public String buy(CartPackageDTO pack) {
		pack.setPurchased(true);
		this.manager.purchase(pack);
		return null;
	}
}
